package NetWork.TCPDome001;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * ClassName:Server
 * Package:NetWork.TCPDome001
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-21:34
 * @Version:v1.0
 */
public class Server {
    /* 服务端
     * 1.创建服务器端的Socket对象(ServerSocket)
     *   ServerSocket(int port)
     * 2.监听客户端连接,返回一个Socket对象
     *   Socket accept()
     * 3.获取输入流,读取数据,并把数据显示在控制台
     *   InputStream getInputStream()
     * 4.释放资源
     *   void close()*/
    public static void main(String[] args) throws IOException {
        //创建对象
        ServerSocket serverSocket = new ServerSocket(1000);

        //监听客户端的连接
        Socket socket = serverSocket.accept();
        //从连接通道获取输入流读取数据
//        InputStream is = socket.getInputStream();
//        InputStreamReader isr = new InputStreamReader(is);
//        BufferedReader br = new BufferedReader(isr);

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int b;
        while ((b = br.read()) != -1) {
            System.out.print((char) b);
        }
        //释放资源
        br.close();
        serverSocket.close();
    }
}
